from django.core.files.uploadedfile import InMemoryUploadedFile
from django.shortcuts import render
from django.http import JsonResponse
from OpenAI.settings import RuiJiang_Base_Url
import requests


# Create your views here.


# 获取云盘信息列表
def getdiskInfo(request=None):
    data = {
        "uuids": None,  # ??????????????????????
        "__token": request.POST.get('token'),
        "__device": "web"
    }
    response = requests.post(f'{RuiJiang_Base_Url}user/client/query', data=data)
    return response.json()


def uploadFile(request):
    FileName = next(iter(request.FILES))
    File: InMemoryUploadedFile = request.FILES[FileName]

    ALLOWED_EXTENSIONS = ['csv', 'txt', 'pdf', 'md', 'docx', 'doc']
    if not File:
        return JsonResponse({"code": 400, "msg": '缺失文件'})

    def allowed_file(File_Name):
        return '.' in File_Name and \
               File_Name.split('.')[1].lower() in ALLOWED_EXTENSIONS
    if not allowed_file(FileName):
        return JsonResponse({"code": 400, "msg": '不允许上传该类型文件'})
    if File.size > 1048576:
        return JsonResponse({"code": 400, "msg": '不允许上传的文件超出1M'})
    with open(f'./File/{FileName}', 'wb') as f:
        f.write(File.read())
    return JsonResponse({"code": 200, "msg": '上传成功'})
